package cn.tedu.yushougeproduct.controller;

import cn.tedu.yushouge.commons.pojo.vo.PageData;
import cn.tedu.yushougeproduct.pojo.entity.ShoppingCart;
import cn.tedu.yushougeproduct.pojo.vo.*;
import cn.tedu.yushougeproduct.service.IShoppingCartService;
import cn.tedu.yushouge.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/shopping-cart/")
@Slf4j
@Api(tags = "04：购物车管理模块")
@Validated
public class ShoppingCartController {

    @Autowired
    IShoppingCartService ishoppingCartService;

    @PostMapping("add-new")
    @ApiOperation("添加购物车")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@RequestParam Long userId) {
        log.debug("开始处理【添加购物车分类】的请求，参数：{}", userId);
        ishoppingCartService.addNew(userId);
        return JsonResult.ok();
    }

    @PostMapping("delete")
    @ApiOperation("根据ID删除购物车")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "购物车ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@Range(min = 1, message = "根据ID删除购物车失败，请提交合法的ID值！")
                                       @RequestParam Long id) {
        log.debug("开始处理【根据ID删除购物车】的请求，参数：{}", id);
        ishoppingCartService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("updateState")
    @ApiOperation("根据ID更新购物车状态")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cartId", value = "购物车ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "stateId", value = "状态ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateState(@Range(min = 1, message = "根据ID删除购物车失败，请提交合法的ID值！")
                                        @RequestParam Long cartId, @RequestParam Long stateId) {
        log.debug("开始处理【根据ID修改购物车状态】的请求，参数：{}", cartId);
        ishoppingCartService.updateShoppingCartState(cartId, stateId);
        return JsonResult.ok();
    }

    @GetMapping("standard")
    @ApiOperation("根据ID查询购物车详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cartId", value = "ID", required = true, dataType = "long")
    })
    public JsonResult<ShoppingCartStandardVO> getStandardById(@Range(min = 1, message = "根据Id查询购物车详情失败，请提交合法的Id值！")
                                                                  @RequestParam Long cartId) throws Exception {
        log.debug("开始处理【根据userId查询购物车详情】的请求，参数：{}", cartId);
        ShoppingCartStandardVO result = ishoppingCartService.getStandardById(cartId);
        return JsonResult.ok(result);
    }

    @GetMapping("standardByUser")
    @ApiOperation("根据用户ID查询购物车详情")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "ID", required = true, dataType = "long")
    })
    public JsonResult<ShoppingCart> getStandardByUserId(@Range(min = 1, message = "根据userId查询购物车详情失败，请提交合法的Id值！")
                                                              @RequestParam Long userId) throws Exception {
        log.debug("开始处理【根据userId查询购物车详情】的请求，参数：{}", userId);
        ShoppingCart result = ishoppingCartService.getByUserId(userId);
        return JsonResult.ok(result);
    }


}
